<style>
th span{white-space:normal;line-height:19px;}
.layui-table-cell {line-height:19px; padding: 3px 0px;text-align:center}
.layui-table-box {
    overflow: inherit;
}
.redclass{color:#CC0000;font-weight:bold;}
.layui-table-body {
    overflow: inherit;
}
.layui-form-item .layui-input-inline{width:200px;}
.search_form .layui-input-inline{width:120px;}
.layui-form-pane .layui-form-label {width: 90px;padding:6px 9px;}
.fixed-bar .layui-input {width:120px;}
.layui-table-view .layui-table td, .layui-table-view .layui-table th{padding:1px 0px}
.people_add{background-color:#D8D8AF !important;}
.quintityChanged,.changedBg{background-color:#FF9900;}
.layui-form-pane .layui-form-label{width:auto;font-weight:bold;}
</style>
<div class="fixed-bar">
    <span style="float:left" id="totaldata_tag">
		<a  href="{:url('editStatements')}?process_stage=" style="display:none" class="layui-btn layui-btn-sm layui-btn-primary">按订单号合并</a>
		<a  href="{:url('editStatements')}?process_stage=" style="display:none" class="layui-btn layui-btn-sm layui-btn-primary">展示详细扎数</a>
	</span>
    <div class="layui-form-pane">
		<form class="layui-form" 
         action="{:url("")}?month={$month}&user_number={$user_number}&action=salaryDetailList&show_data={$show_data}" method="get" id="hisi-table-search">
			<div class="layui-inline">
				<strong>搜索：</strong>
				<input type="hidden" name="old_order_id" value="{:$order_id}">
			</div>
			<div class="layui-inline">
                <label class="layui-form-label">订单号</label>
                <div class="layui-input-inline" >
                    <input type="text" name="client_ordernum" lay-verify="" placeholder="搜索订单号" class="layui-input" style="width:130px;">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">工序</label>
                <div class="layui-input-inline">
                    <input type="text" name="process_name" lay-verify="" placeholder="工序名称" class="layui-input" style="width:130px;">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">SKU</label>
                <div class="layui-input-inline">
                    <input type="text" name="sku" lay-verify="" placeholder="输入SKU" class="layui-input" style="width:190px;">
                </div>
            </div>
			<div class="layui-inline">
                <button type="submit" id="seacher" class="layui-btn" lay-submit="" lay-filter="demo1">搜索</button>
            </div>
		</form>
    </div>
</div>
<table id="dataTable" lay-filter="filterTableSFD"></table>
{include file="system@block/layui" /}
<script type="text/html" id="stallOrderTimeTpl">
	{{# if(d.work_time=='总计'){ }}  
        <span style="font-weight:bold;">总计：</span>
    {{# }else{ }}
        {{layui.util.toDateString(d.work_time * 1000, 'yyyy-MM-dd HH:mm')}}
    {{# } }}  
</script>
<script type="text/html" id="usernameTpl">
	<input type="hidden" name="createrNoPut" id="process_data_{{ d.id }}" value="{{ d.creater}}" />
    <span class="copy-item">{{ d.user_name }}</span> /    <span class="copy-item">{{ d.user_number }}</span>
</script>
<script type="text/html" id="caigouUnitPriceTpl">
    {{# if(parseFloat(d.caigou_unitprice) > parseFloat(d.customer_price)){ }}  
        <span style="color:red">{{d.caigou_unitprice}}</span>
    {{# }else{ }}
        {{d.caigou_unitprice}}
    {{# } }}    
</script>
<script type="text/html" id="subbtnTpl">
    {{# if(d.check_state <= 1){ }}
		<a class="layui-btn layui-btn-xs layui-btn-danger" style="margin-left:5px;" lay-event="delete">删除</a>
    {{# }else if(d.check_state == 2 || d.check_state == 3){ }}
       已审核
    {{# } }}

</script>
<script type="text/html" id="toolbar">
	<span class="finance_operate">
			<a data-href="{:url('setStatementsSettle')}?process_stage=1&status=1" class=" layui-btn layui-btn-primary layui-btn-sm j-page-btns confirm layui-icon back">退回到对账中</a>&nbsp;&nbsp;
	</span>
</script>

<script type="text/html" id="billActionTpl">

</script>

<script type="text/javascript">
    layui.use(['opTable','table','jquery', 'tool','laytpl', 'form','laydate', 'layer'], function() {
        var $ = layui.jquery;
        var table = layui.table;
        var laytpl=layui.laytpl;
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
		form.render();
        
		var nowtime=new Date();
		var  value=nowtime.getFullYear()+"-"+(nowtime.getMonth()+1)+"-01";

		
        var opTable=table.render({
            elem: '#dataTable'
            ,url:'{:url("")}?month={$month}&user_number={$user_number}&order_id={$order_id}&process_name={$process_name}&action=salaryDetailList&show_data={$show_data}' 
            ,page: true //开启分页
            ,skin: 'row'
            ,even: true
            ,limit: 40
            ,limits: [40,50,100,150,200,500,1000,2000]
            ,text: {
                none : '暂无相关数据'
            }
			,defaultToolbar: ['filter','exports','print']
			,toolbar: '#toolbar'
            ,cols: [[ //表头
				{type:'checkbox'},
                {field:'work_time',title : '完成日期',templet: '#stallOrderTimeTpl',width:120}
                ,{field: 'user_name', title: '姓名/编号',templet:'#usernameTpl',width:110}
                ,{field:'client_ordernum',title : '订单号',width:130}
				,{field:'sku',title : 'sku',width:120}
				,{field: 'quantity', title: '订单总数'}
                ,{field: 'shicai_count', title: '实裁数',templet:function(d){ return d.shicai_count;}}
				,{field: 'process_name', title: '工序'}
                ,{field:'process_quintity',title : '加工数量'}
				//,{field:'process_count',title : '累计结算数',templet:function(d){ var classval=Number(d.process_count)>Number(d.shicai_count)? 'redclass' :'';
				//return "<span class='"+classval+"'>"+d.process_count+"</span>";}}
				,{field:'tot_quintity',title : '结算数量',templet:function(d){ classval=Number(d.old_quintity)>0? 'quintityChanged' :'';return "<span class='"+classval+"'>"+d.tot_quintity+"</span>";}} 
                ,{field:'process_price',title : '单价'}
                ,{field:'money_total',title : '结算金额',templet:function(d){return "<span id='money_total"+d.id+"'>"+d.money_total+"</span>";}}
				,{field:'check_remark',title : '审核备注',width:150}
				,{field:'last_editer',title : '最后编辑'}
				,{field:'action',title : '操作',templet:'#subbtnTpl'}
            ]]
            ,done: function (res) {
				var data=res.data,mInfo=res.mInfo;
				laydate.render({
					elem: "#work_time", //指定元素
					value: '' 
				});
            }
        });
        table.on('edit(filterTableSFD)', function(obj){
            var value = obj.value //得到修改后的值
                ,d = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            var caigouUnitprice=0;
            if(field == 'tot_quintity' || field == 'process_price'){
                tot_quintity=d.tot_quintity;
                var caigou_amount = layui.tool.bcmul(d.tot_quintity,d.process_price);
                obj.update({money_total:caigou_amount});
            }
			var changeInfo=JSON.parse($(".changeQuantityLabel").html()); //转成json对像
			if($(".changeQuantityLabel").html()=='[]'){
				changeInfo=new Array();
			}
			for(var key in changeInfo){
				if(changeInfo[key]['id']==d.id){
					changeInfo.splice(key,key+1);
				};
			}
			changeInfo.push(d);
			changeInfo=JSON.stringify(changeInfo);//将json对象转换成json字符串
			$(".changeQuantityLabel").html(changeInfo);
		

        });
		table.on('tool(filterTableSFD)', function(obj){
            var d = obj.data,field=obj.field;
            if(obj.event === 'delete'){
				if(confirm('确定要删除该加工信息吗')==false){
					return;
				}
				$.ajax({
					type: "POST",
					url: "{:url('addBills')}?action=delete",
					data: "id="+d.id+"&process_name=111",
					dataType:"json",
					success: function(res){
						if (res.code == 1) {
							layer.msg(res.msg, {time:600,icon: 1});
							parent.reloadTable();
							obj.tr[0].remove();
							
							//opTable.reload();
						} else {
							layer.msg(res.msg, {icon: 2,anim: 6});
						}
					}
				});
				
			}
		});

        window.saveChage = function () {
            var changedatastr = $(".changeQuantityLabel").html();
            var chagedata={changelist:changedatastr};
			var edit_data2 = form.val("formTest");
			edit_data2.changelist=changedatastr;
			//console.log(edit_data2);
            if(changedatastr.length<=5){
                //alert('当前未做任何调整');return;
            }
            $.ajax({
                type: "POST",
                url: "{:url('doEditStatementsProcessPrice')}?action=changeQuintity",
                data: edit_data2,
                dataType:"json",
                success: function(res){
                    if (res.code == 1) {
                        layer.msg(res.msg, {icon: 1,time:600,end:function(){
							opTable.reload();
							if(edit_data2.user_number!='') $("#old_user_number").val(edit_data2.user_number);//更换成新条件供后续编辑
							if(edit_data2.process_name!='') $("#old_process_name").val(edit_data2.process_name);
						
						}});
						parent.reloadTable();
                    } else {
                        layer.msg(res.msg, {icon: 2,anim: 6});
                    }
                }
            });
            return false;
        }

        // 单元格修改数据校验
		window.checkData = function(d) {
			var caigouUnitprice = d.tot_quintity;
        	var billingCaigouKgprice = d.process_price;
            var stallFeedType = d.stall_feed_type;
        	var reg = /((^([-+])?[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;
        	if (!reg.test(caigouUnitprice) || d.tot_quintity<=0) {
        		layer.msg('加工数量输入有误', {icon: 2,anim: 6});
        		return false;
        	}
        	if (!reg.test(billingCaigouKgprice)) {
        		layer.msg('工序单价输入有误', {icon: 2,anim: 6});
        		return false;
        	}
        	return true;
		}

    });
</script>